// pages/searchRaisingPets/searchRaisingPets.js

var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    HotArticle:[],
    total: 1,
    ArticleList: [],
    search:"",
  },
  content:function(e){
    that.setData({ search: e.detail.value });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;

    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    setTimeout(() => {
      that.getHotArticleSearchLabel();
      //that.getYangchongArticleList();
    }, 1500)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    if (that.data.search == ""){
      wx.stopPullDownRefresh();
        return;
      }

    that.data.total = 1;
    // this.data.dataArray 是页面中绑定的数据源
    that.data.collectArticleList = [];


    // 网络请求，重新请求一遍数据
    that.getYangchongArticleList();
    // 小程序提供的api，通知页面停止下拉刷新效果

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (that.data.search == "") {
      wx.stopPullDownRefresh();
      return;
    }

    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    that.data.total = that.data.total + 1;
    setTimeout(() => {
      that.getYangchongArticleList();
      wx.stopPullDownRefresh();
    }, 1500)
  },

 
  getHotArticleSearchLabel:function(){
    wx.request({
      url: getApp().globalData.urls + '/wxgz/article/getHotArticleSearchLabel',
      data: {
        userType:1
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success:function(res){
        console.log(res.data)
        if(res.data.code == 200){
            that.setData({
              HotArticle:res.data.data
            })
        }
      }
    })
  },

  search:function(){
    that.getYangchongArticleList();
  },


  getYangchongArticleList: function () {
    console.log(that.data.search)
    wx.request({
      url: getApp().globalData.urls + '/wxgz/article/getYangchongArticleList',

      data: {
        page: that.data.total,
        rows: '10',
        search: that.data.search
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        console.log(res.data)

        if (res.data.code == 200) {
          var datas = res.data.data;

          for (let i = 0; i < datas.length; i++) {
            //datas[i].created = that.jsonTimeStamp(datas[i].created)

            var datetime = new Date();
            datetime.setTime(datas[i].releaseTime);
            var year = datetime.getFullYear();
            var month = datetime.getMonth() + 1 < 10 ? "0"
              + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
            var date = datetime.getDate() < 10 ? "0" + datetime.getDate()
              : datetime.getDate();
            var hour = datetime.getHours() < 10 ? "0" + datetime.getHours()
              : datetime.getHours();
            var minute = datetime.getMinutes() < 10 ? "0"
              + datetime.getMinutes() : datetime.getMinutes();
            var second = datetime.getSeconds() < 10 ? "0"
              + datetime.getSeconds() : datetime.getSeconds();
            datas[i].releaseTime = year + "-" + month + "-" + date + " " + hour + ":" + minute
              + ":" + second;
          }

          var list = that.data.ArticleList;
          for (var i = 0; i < datas.length; i++) {
            list.push(datas[i]);
          }

          that.setData({
            ArticleList: datas
          })

        }

      }



    })
  },
  hotItem:function(e){
    that.setData({ search: e.currentTarget.dataset.data });
    that.getYangchongArticleList();
  },

  aaaa: function (e) {
    console.log(e.currentTarget.dataset.item)
    var data = e.currentTarget.dataset.item
    var id = data.id
    //var id = 24
    //let data = JSON.stringify(e.currentTarget.dataset.data); 
    //console.log(data)
    wx.navigateTo({
      url: '../articleDetail/articleDetail?id=' + id
    })
  }
})